﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Xml.Serialization;
namespace OrderApplication
{
    public class OrderMana
    {
        static void Main(string[] args)
        {
            OrderService orders = new OrderService();
            bool flag = true;
            while (flag)
            {
                Console.WriteLine("------1.添加订单------");
                Console.WriteLine("------2.移除订单------");
                Console.WriteLine("------3.查询订单------");
                Console.WriteLine("------4.显示订单------");
                Console.WriteLine("------5.导出订单------");
                Console.WriteLine("------6.导入订单------");
                Console.WriteLine("------7.退出系统------");
                Console.Write("请选择操作（数字）：");
                string input = Console.ReadLine();
                switch (input)
                {
                    case "1":
                        orders.addOrder();
                        break;
                    case "2":
                        orders.removeOrder();
                        break;
                    case "3":
                        orders.searchOrder();
                        break;
                    case "4":
                        orders.showOrders();
                        break;
                    case "5":
                        orders.export();
                        break;
                    case "6":
                        orders.import();
                        break;
                    case "7":
                        flag = false;
                        break;
                    default:
                        Console.WriteLine("输入错误! ");
                        Console.WriteLine("请按任意键继续...");
                        Console.ReadKey();
                        break;
                }
            }
        }
    }

    public interface IOrderService
    {
        void addOrder();
        void removeOrder();
        void searchOrder();
        void showOrders();
    }

    public class OrderDetails
    {
        private string name;
        public string Name { get; set; }
        private int number;
        public int Number { get; set; }
        private double price;
        public double Price { get; set; }

        public OrderDetails()
        {
            this.Name = string.Empty;
            this.Number = 0;
            this.Price = 0;
        }

        public OrderDetails(string name, int number, double price)
        {
            this.Name = name;
            this.Number = number;
            this.Price = price;
        }

        public double getAllPrice()
        {
            return Price * Number;
        }
    }

    public class Order : IComparable
    {
        public int Id { get; set; }
        public string Customer { get; set; }
        public DateTime Date { get; set; }
        public double Money { get; set; }

        public List<OrderDetails> orderDetails = new List<OrderDetails>();

        public Order()
        {
            this.Id = 0;
            this.Customer = string.Empty;
            this.Date = DateTime.MinValue;
            this.Money = 0;
        }

        public Order(int id, string customer, DateTime date)
        {
            this.Id = id;
            this.Customer = customer;
            this.Date = date;
            this.Money = 0;
        }

        public void setMoney()
        {
            double money = 0;
            foreach (OrderDetails details in orderDetails)
            {
                money += details.getAllPrice();
            }
            this.Money = money;
        }

        public void addOrderDetails(string name, int number, double price)
        {
            OrderDetails details = new OrderDetails(name, number, price);
            this.orderDetails.Add(details);
        }

        public void removeOrderDetails()
        {
            Console.Write("请输入待删除订单编号ID：");
            int id = Convert.ToInt32(Console.ReadLine());
            this.orderDetails.RemoveAt(id);
            Console.WriteLine("成功移除！");
            Console.WriteLine("输入任意键继续...");
            Console.ReadKey();
            Console.Clear();
        }

        public void showOrderDetails()
        {
            foreach (OrderDetails details in this.orderDetails)
            {
                Console.WriteLine("名称\t 数量\t 单价");
                Console.WriteLine("{0}\t {1}\t {2}",
                    details.Name, details.Number, details.Price);
            }
        }

        #region IComparable Members
        public int CompareTo(object? obj)
        {
            if (obj != null)
            {
                Order? order = obj as Order;
                return this.Id.CompareTo(order?.Id);
            }
            else
                return -1;
        }

        public override bool Equals(object? obj)
        {
            Order? order = (obj as Order);
            return this.Id.Equals(order?.Id);
        }

        public override int GetHashCode()
        {
            return Convert.ToInt32(this.Id);
        }
    }

    public class OrderService : IOrderService
    {
        public List<Order> orders = new List<Order>();

        public OrderService() { }

        public void export()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fileStream = new FileStream("OrderList.xml", FileMode.Create))
            {
                xmlSerializer.Serialize(fileStream, this.orders);
            }
            Console.WriteLine("Xml序列化成功！");
            Console.WriteLine("输入任意键继续...");
            Console.ReadKey();
            Console.Clear();
        }

        public void import()
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fileStream = new FileStream("OrderList.xml", FileMode.Open))
            {
                orders = (List<Order>)xmlSerializer.Deserialize(fileStream)!;
            }
            Console.WriteLine("输入任意键继续...");
            Console.ReadKey();
            Console.Clear();
        }

        public void addOrder()
        {
            try
            {
                Console.Write("请输入待添加订单编号ID：");
                int id = Convert.ToInt32(Console.ReadLine());
                Console.Write("请输入待添加订单顾客Customer：");
                string customer = Console.ReadLine();
                Console.WriteLine("请输入待添加订单日期Date：");
                Console.Write("\t请输入年份Year：");
                int year = Convert.ToInt32(Console.ReadLine());
                Console.Write("\t请输入月份Month：");
                int month = Convert.ToInt32(Console.ReadLine());
                Console.Write("\t请输入日份Day：");
                int day = Convert.ToInt32(Console.ReadLine());
                DateTime date = new DateTime(year, month, day);
                Order order = new Order(id, customer, date);

                bool judge = true;
                bool same = false;
                foreach (Order o in orders)
                {
                    if (o.Equals(order))
                        same = true;
                }
                if (same)
                {
                    Console.WriteLine("订单号输入重复！");
                    Console.WriteLine("输入任意键继续...");
                    Console.ReadKey();
                    Console.Clear();
                }
                else
                {
                    if (!same)
                    {
                        Console.Write("输入商品名称：");
                        string name = Console.ReadLine();
                        Console.Write("输入商品数目：");
                        int number = Convert.ToInt32(Console.ReadLine());
                        Console.Write("输入商品单价：");
                        double price = Convert.ToDouble(Console.ReadLine());
                        order.addOrderDetails(name, number, price);
                    }
                    orders.Add(order);
                    order.setMoney();
                    Console.WriteLine("订单添加成功！");
                    Console.WriteLine("输入任意键继续...");
                    Console.ReadKey();
                    Console.Clear();
                }

            }
            catch
            {
                Console.WriteLine("输入错误！");
                Console.WriteLine("输入任意键继续...");
                Console.ReadKey();
                Console.Clear();
            }
        }

        public void removeOrder()
        {
            try
            {
                Console.Write("请输入待删除订单编号ID：");
                int id = Convert.ToInt32(Console.ReadLine());
                int index = 0;
                foreach (Order order in orders)
                {
                    if (order.Id == id)
                        index = this.orders.IndexOf(order);
                }
                this.orders[index].showOrderDetails();
                this.orders[index].removeOrderDetails();
                this.orders.RemoveAt(index);
                Console.WriteLine("成功移除！");
                Console.WriteLine("输入任意键继续...");
                Console.ReadKey();
                Console.Clear();
            }
            catch
            {
                Console.WriteLine("输入错误！");
                Console.WriteLine("输入任意键继续...");
                Console.ReadKey();
                Console.Clear();
            }
        }

        public void searchOrder()
        {
            Console.WriteLine("1.根据编号查询订单");
            Console.WriteLine("2.根据顾客查询订单");
            Console.WriteLine("3.根据日期查询订单");
            Console.WriteLine("4.根据金额查询订单");
            Console.Write("请选择操作：");
            int i = Convert.ToInt32(Console.ReadLine());
            try
            {
                switch (i)
                {
                    case 1:
                        Console.Write("输入待查询订单编号：");
                        int id = Convert.ToInt32(Console.ReadLine());

                        var queryInId = from order in this.orders
                                              where order.Id == id
                                              orderby order.Id
                                              select order;

                        List<Order> orderQueryInId = queryInId.ToList();
                        foreach (Order order in orderQueryInId)
                        {
                            Console.WriteLine("编号\t 顾客\t 日期\t\t\t 金额");
                            Console.WriteLine("{0}\t {1}\t {2}\t {3}",
                                order.Id, order.Customer, order.Date, order.Money);
                            order.showOrderDetails();
                        }
                        Console.WriteLine("输入任意键继续...");
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    case 2:
                        Console.Write("输入待查询订单顾客：");
                        string customer = Console.ReadLine();

                        var queryInCustomer = from order in this.orders
                                              where order.Customer == customer
                                              orderby order.Id
                                              select order;
                        List<Order> orderQueryInCustomer = queryInCustomer.ToList();
                        foreach (Order order in orderQueryInCustomer)
                        {
                            Console.WriteLine("编号\t 顾客\t 日期\t\t\t 金额");
                            Console.WriteLine("{0}\t {1}\t {2}\t {3}",
                                order.Id, order.Customer, order.Date, order.Money);
                            order.showOrderDetails();
                        }
                        Console.WriteLine("输入任意键继续...");
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    case 3:
                        Console.WriteLine("输入待查询订单日期：");
                        Console.Write("\t请输入年份Year：");
                        int year = Convert.ToInt32(Console.ReadLine());
                        Console.Write("\t请输入月份Month：");
                        int month = Convert.ToInt32(Console.ReadLine());
                        Console.Write("\t请输入日份Day：");
                        int day = Convert.ToInt32(Console.ReadLine());
                        DateTime date = new DateTime(year, month, day);

                        var queryInDate = from order in this.orders
                                              where order.Date == date
                                              orderby order.Id
                                              select order;
                        List<Order> orderQueryInDate = queryInDate.ToList();
                        foreach (Order order in orderQueryInDate)
                        {
                            Console.WriteLine("编号\t 顾客\t 日期\t\t\t 金额");
                            Console.WriteLine("{0}\t {1}\t {2}\t {3}",
                                order.Id, order.Customer, order.Date, order.Money);
                            order.showOrderDetails();
                        }
                        Console.WriteLine("输入任意键继续...");
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    case 4:
                        Console.Write("输入待查询订单金额：");
                        int money = Convert.ToInt32(Console.ReadLine());

                        var queryInMoney = from order in this.orders
                                        where order.Money == money
                                        orderby order.Id
                                        select order;

                        List<Order> orderQueryInMoney = queryInMoney.ToList();
                        foreach (Order order in orderQueryInMoney)
                        {
                            Console.WriteLine("编号\t 顾客\t 日期\t\t\t 金额");
                            Console.WriteLine("{0}\t {1}\t {2}\t {3}",
                                order.Id, order.Customer, order.Date, order.Money);
                            order.showOrderDetails();
                        }
                        Console.WriteLine("输入任意键继续...");
                        Console.ReadKey();
                        Console.Clear();
                        break;
                    default:
                        Console.WriteLine("输入错误！");
                        Console.WriteLine("输入任意键继续...");
                        Console.ReadKey();
                        Console.Clear();
                        break;
                }
            }
            catch
            {
                Console.WriteLine("输入错误！");
            }
        }

        public void showOrders()
        {
            foreach (var order in orders)
            {
                Console.WriteLine("编号\t 顾客\t 日期\t\t\t 金额");
                Console.WriteLine("{0}\t {1}\t {2}\t {3}",
                    order.Id, order.Customer, order.Date, order.Money);
                order.showOrderDetails();
            }
            Console.WriteLine("输入任意键继续...");
            Console.ReadKey();
            Console.Clear();
        }
    }
}
#endregion 